package com.xiaotu.spider.project.pageprocessor.list;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 ImDBListPageProcessor
 * @日期 2017年8月14日
 * @作者 王艳龙
 * @功能 ImDB网站数据爬取
 */

@Component("ImDBListPageProcessor")
@Scope("prototype")
public class ImDBListPageProcessor extends AUrlPageProcessor {

	private String fileName; // 存放文件路径

	@Override
	protected String[] getStartRequestUrls() {
		return new String[] { "http://www.imdb.com/chart/moviemeter?sort=rk,asc&mode=simple&page=1" };
	}

	@Override
	public void initRunParams(Map<String, Object> map) {
		if (map.get("fileName") == null)
			throw new SpiderException(SpiderExceptionCode.PageProcessorParameterErrorException);
		fileName = map.get("fileName") + StringUtils.EMPTY;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		if (url.contains("http://www.imdb.com/chart/moviemeter")) {
			return Constants.PageDataType.SEARCH_LIST;
		} else if (url.contains("pf_rd_m")) {
			return Constants.PageDataType.TV_RECORD;
		} else {
			return Constants.PageDataType.SEARCH_LIST;
		}

	}

	@Override
	public void process(Page page) {
		Map<String, Object> tvMap = page.getRequest().getExtras();
		switch (this.getPageDataType(page)) {
		case Constants.PageDataType.SEARCH_LIST:
			this.searchProcess(page);
			break;
		case Constants.PageDataType.TV_RECORD:
			this.detailsProcess(page, tvMap);
			break;
		default:
			break;
		}
	}

	public void searchProcess(Page page) {
		List<String> divList = page.getHtml().xpath("//td[@class='titleColumn']/a/@href").all();
		if (divList == null || divList.isEmpty())
			throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		for (String links : divList) {
			PageUtils.addTargetRequest(page, links, page.getRequest().getExtras());
		}
	}

	// 电影详情页
	public void detailsProcess(Page page, Map<String, Object> tvMap) {
		StringBuffer buffer = new StringBuffer();
		// 电影名称
		String movieName = page.getHtml().xpath("//div[@class='title_wrapper']/h1/text()").get().trim();
		if (StringUtils.isEmpty(movieName)) {
			movieName = SepratorUtil.SEP_SPACE_EN;
		}
		buffer.append(movieName);
		// 评分
		String movieScore = page.getHtml().xpath("//div[@class='ratingValue']/strong/span/text()").get();
		if (StringUtils.isEmpty(movieScore)) {
			movieScore = SepratorUtil.SEP_SPACE_EN;
		}
		buffer.append(SepratorUtil.SEP_PIPE + movieScore);
		// 参加评论人数
		String movieComNum = page.getHtml().xpath("//div[@class='imdbRating']/a/span/text()").get();
		if (StringUtils.isEmpty(movieComNum)) {
			movieComNum = SepratorUtil.SEP_SPACE_EN;
		}
		buffer.append(SepratorUtil.SEP_PIPE + movieComNum);
		// 用户评论总量与专业人士评论数量
		List<String> movieComCountExpert = page.getHtml()
				.xpath("//div[@class='titleReviewBarItem titleReviewbarItemBorder']/div/span[@class='subText']/a/text()")
				.all();
		if(movieComCountExpert.size()>0){
			for (String comCountExpertStr : movieComCountExpert) {
				if (comCountExpertStr.contains("user")) {
				// 用户评论总量
				comCountExpertStr = comCountExpertStr.replace("user", "").trim();
				buffer.append(SepratorUtil.SEP_PIPE + comCountExpertStr);
				
				}if (comCountExpertStr.contains("critic")) {
				// 专业人士评论数量
				comCountExpertStr = comCountExpertStr.replace("critic", "").trim();
				buffer.append(SepratorUtil.SEP_PIPE + comCountExpertStr);
				}
			}
		}else{
			buffer.append(SepratorUtil.SEP_PIPE + SepratorUtil.SEP_SPACE_EN 
					+ SepratorUtil.SEP_PIPE+ SepratorUtil.SEP_SPACE_EN);
		}
		List<String> PopularityList = page.getHtml().xpath("//div[@class='titleReviewBarItem']").all();
		for (String PopularityStr : PopularityList) {
			Html htmlStr = new Html(PopularityStr);
			if (PopularityStr.contains("popularityTrendDown")) {
				// 受欢迎程度
				String moviePopularity = htmlStr
						.xpath("//div[@class='titleReviewBarSubItem']/div/span[@class='subText']/text()").get();
				if (StringUtils.isEmpty(moviePopularity)) {
					moviePopularity = SepratorUtil.SEP_SPACE_EN;
				} else {
					moviePopularity = moviePopularity.replace("( )", "").trim();
				}
				buffer.append(SepratorUtil.SEP_PIPE + moviePopularity);
				// 受欢迎程度下降
				String popularityChange = htmlStr.xpath("//span[@class='popularityDown']/text()").get();
				if (StringUtils.isEmpty(popularityChange)) {
					popularityChange = SepratorUtil.SEP_SPACE_EN;
				}
				buffer.append(SepratorUtil.SEP_PIPE + SepratorUtil.SEP_CROSS_EN + popularityChange);

			} else if (PopularityStr.contains("popularityTrendUp")) {
				// 受欢迎程度
				String moviePopularity = htmlStr
						.xpath("//div[@class='titleReviewBarSubItem']/div/span[@class='subText']/text()").get();
				if (StringUtils.isEmpty(moviePopularity)) {
					moviePopularity = SepratorUtil.SEP_SPACE_EN;
				} else {
					moviePopularity = moviePopularity.replace("( )", "").trim();
				}
				buffer.append(SepratorUtil.SEP_PIPE + moviePopularity);
				// 受欢迎程度上升
				String popularityChange = htmlStr.xpath("//span[@class='popularityUpOrFlat']/text()").get();
				if (StringUtils.isEmpty(popularityChange)) {
					popularityChange = SepratorUtil.SEP_SPACE_EN;
				}
				buffer.append(SepratorUtil.SEP_PIPE + SepratorUtil.SEP_PLUS_EN + popularityChange);
			}
		}

		// MAPP评级
		String movieMAPP = page.getHtml().xpath("//div[@class='txt-block']/span[@itemprop='contentRating']/text()")
				.toString();
		if (StringUtils.isEmpty(movieMAPP)) {
			movieMAPP = SepratorUtil.SEP_SPACE_EN;
		}
		buffer.append(SepratorUtil.SEP_PIPE + movieMAPP);
		// 电影预算、上映首周票房、首周票房截止日期、总票房，总票房截止日期
		List<String> divList = page.getHtml().xpath("//div[@class='article']/div[@class='txt-block']").all();
		if (divList.toString().contains("Budget") || divList.toString().contains("Opening") || divList.toString().contains("Gross")) {
			for (String divStr : divList) {
				Html htmlStr = new Html(divStr);
				String types = htmlStr.xpath("//h4[@class='inline']/text()").get();
				if (StringUtils.isEmpty(types)) {
					continue;
				}
				// 电影预算
				if (types.contains("Budget")) {
					String movieBudget = htmlStr.xpath("//div[@class='txt-block']/text()").get().trim();
					if(StringUtils.isEmpty(movieBudget)){
						movieBudget = SepratorUtil.SEP_SPACE_EN;
					}
					buffer.append(SepratorUtil.SEP_PIPE + movieBudget);

				} else if (types.contains("Opening Weekend")) {
					// 上映周票房
					String movieOpenWeek = htmlStr.xpath("//div[@class='txt-block']/text()").get();
					if(StringUtils.isEmpty(movieOpenWeek)){
						movieOpenWeek = SepratorUtil.SEP_SPACE_EN;
					}else{
						movieOpenWeek = movieOpenWeek.replace("(USA)", "").trim();
					}
					buffer.append(SepratorUtil.SEP_PIPE + movieOpenWeek);
					// 上映周票房截止日期
					String movieWeekDate = htmlStr.xpath("//span[@class='attribute']/text()").get();
					if(StringUtils.isEmpty(movieWeekDate)){
						movieWeekDate = SepratorUtil.SEP_SPACE_EN;
					}
					buffer.append(SepratorUtil.SEP_PIPE + movieWeekDate);

				} else if (types.contains("Gross")) {
					// 上映总票房
					String movieGross = htmlStr.xpath("//div[@class='txt-block']/text()").get();
					if(StringUtils.isEmpty(movieGross)){
						movieGross = SepratorUtil.SEP_SPACE_EN;
					}else{
						movieGross = movieGross.trim();
					}
					buffer.append(SepratorUtil.SEP_PIPE + movieGross);
					// 上映总票房截止日期
					List<String> movieGrossDates = htmlStr.xpath("//span[@class='attribute']/text()").all();
					if (movieGrossDates.size() > 0) {
						movieGrossDates.remove(0);
						for (String movieGrossDate : movieGrossDates) {
							buffer.append(SepratorUtil.SEP_PIPE + movieGrossDate);
						}
					}else{
						buffer.append(SepratorUtil.SEP_SPACE_EN+SepratorUtil.SEP_PIPE);
					}

				} else if (types.contains("Production")) {
					String links = htmlStr.xpath("//span[@class='see-more inline']/a/@href").get();
					String tvinfo = HttpUtils.httpGetJSONP(links);
					Html htmls = new Html(tvinfo);
					// 制作公司
					List<String> movieProductions = htmls
							.xpath("//div[@id='company_credits_content']/ul[@class='simpleList']").all();
					StringBuffer sub = new StringBuffer();
					if (movieProductions.size() > 0) {
						String production = movieProductions.get(0);
						Html prodHtml = new Html(production);
						List<String> movieProductionList = prodHtml.xpath("//li/a/text()").all();
						for (String movieProduction : movieProductionList) {
							sub.append(movieProduction + SepratorUtil.SEP_COMMA_EN);
						}
						String movieProducti = sub.toString();
						buffer.append(SepratorUtil.SEP_PIPE + movieProducti);
					}else{
						buffer.append(SepratorUtil.SEP_SPACE_EN+SepratorUtil.SEP_PIPE);
					}
				}
			}
		} else {
			buffer.append(SepratorUtil.SEP_PIPE + SepratorUtil.SEP_SPACE_EN + SepratorUtil.SEP_PIPE
					+ SepratorUtil.SEP_SPACE_EN + SepratorUtil.SEP_PIPE + SepratorUtil.SEP_SPACE_EN
					+ SepratorUtil.SEP_PIPE + SepratorUtil.SEP_SPACE_EN + SepratorUtil.SEP_PIPE
					+ SepratorUtil.SEP_SPACE_EN + SepratorUtil.SEP_PIPE + SepratorUtil.SEP_SPACE_EN);
		}

		// 输出csv文件
		FileUtil.writeFile(fileName, buffer.toString() + SepratorUtil.LINE_SEPRATOR);
	}

	@Override
	protected String getSpiderType() {
		return "ImDBList";
	}
}
